package com.controller.tm;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.common.DateUtils;
import com.common.http.HttpResult;
import com.model.tm.PlanModel;
import com.service.tm.PlanService;

/**
 * 
 */
@Controller("tmplanController")
@RequestMapping(value = "/api")
public class PlanController {
	static final Logger logger = LoggerFactory.getLogger(PlanController.class);

	@Autowired
	PlanService planService;

	/**
	 * 查询列表
	 *
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "tm/plan/selectContractList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectContractList(@RequestParam Map<String, Object> map) {
		return planService.selectContractList(map);
	}

	/**
	 * 查询列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "tm/plan/selectList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectList(@RequestParam Map<String, Object> map) {
		return planService.selectList(map);
	}

	/**
	 * 查询详情
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "tm/plan/selectInfor", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult selectInfor(@RequestParam String planId) {
		return planService.selectInfor(planId);
	}

	/**
	 * 查询轨迹
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "tm/plan/openTmUrl", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult openTmUrl(@RequestParam String transportCode, @RequestParam String tmCarrierName) {
		return planService.openTmUrl(transportCode, tmCarrierName);
	}

	/**
	 * 保存
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "tm/plan/save", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult save(@RequestBody PlanModel planModel) {
		return planService.save(planModel);
	}

	/**
	 * 提交
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "tm/plan/commit", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult commit(@RequestBody PlanModel planModel) {
		return planService.commit(planModel);
	}

	/**
	 * 提交
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "tm/plan/check", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult check(@RequestBody JSONObject hashMap) {
		return planService.check(hashMap);
	}

	/**
	 * 删除
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "tm/plan/delete", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult delete(@RequestParam String planId) {
		return planService.delete(planId);
	}

	/**
	 * 撤回
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "tm/plan/back", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult back(@RequestParam String planId) {
		return planService.back(planId);
	}

	@RequestMapping(value = "tm/plan/getGodNum", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult getGodNum(@RequestParam String planId) {
		return planService.getGodNum(planId);
	}

	@RequestMapping(value = "tm/plan/getTransportCode", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult getTransportCode(@RequestParam String planId) {
		return planService.getTransportCode(planId);
	}

	@RequestMapping(value = "tm/plan/getEms", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult getEms(@RequestParam String planId) {
		return planService.getEms(planId);
	}

	/**
	 * 取消
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "tm/plan/cansel", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult cansel(@RequestParam String planId, @RequestParam String planExcRemark) {
		return planService.cansel(planId, planExcRemark);
	}

	/**
	 * 转卸
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "tm/plan/transfer", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult transfer(@RequestParam String planId, @RequestParam String planExcRemark) {
		return planService.transfer(planId, planExcRemark);
	}

	@RequestMapping(value = "tm/plan/transfercopy", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult transfercopy(@RequestParam String planId, @RequestParam String planExcRemark) {
		return planService.transfercopy(planId, planExcRemark);
	}

	/**
	 * 转卸
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "tm/plan/copy", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult copy(@RequestParam String planId) {
		return planService.copy(planId);
	}

	/**
	 * 导出
	 * 
	 * @param map
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "tm/plan/export")
	public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("采购订单-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		planService.export(map, response);

	}
}
